﻿using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace CloudStorageLight.SharePointWeb.Models
{
    public class SystemSettings
    {

        public static SystemSettings Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = LoadFromSystem();
                }
                return _instance;
            }
            set { _instance = value; }
        }
        private static SystemSettings _instance;
        
        public string BlobStorage { get; set; }

        public string BlobContainerName { get; set; }

        public bool AzureAdMode { get; set; }

        public string Authority { get; set; }

        public string CloudStorageLightClientId { get; set; }

        public string CloudStorageLightAppKey { get; set; }

        public string TenantId { get; set; }

        public string AdminRoleName { get; set; }

        public string LoginSpsSiteUrl { get; set; }

        public bool BatchRole { get; set; }

        public bool MultiDomain { get; set; }

        public bool FulltextSearch { get; set; }

        public string DropBoxAppKey { get; set; }
        public string DropBoxAppSecret { get; set; }

        public string OneDriveClientId { get; set; }
        public string OneDriveClientSecret { get; set; }

        public string GoogleDriveClientId { get; set; }
        public string GoogleDriveClientSecret { get; set; }

        public static SystemSettings LoadFromSystem()
        {
            var newObj = new SystemSettings();
            var multiDomain = GetKeyValue("MultiDomain");
            newObj.MultiDomain = multiDomain == null ? false : bool.Parse(multiDomain);
            newObj.BlobStorage = GetKeyValue("BlobStorage");
            newObj.BlobContainerName = GetKeyValue("BlobContainerName") ?? "files";
            newObj.Authority = GetKeyValue("Authority");
            newObj.CloudStorageLightClientId = GetKeyValue("CloudStorageLightClientId");
            newObj.CloudStorageLightAppKey = GetKeyValue("CloudStorageLightAppKey");
            newObj.TenantId = GetKeyValue("TenantId");
            newObj.AdminRoleName = GetKeyValue("AdminRoleName") ?? "admin";
            newObj.LoginSpsSiteUrl = GetKeyValue("LoginSpsSiteUrl");
            
            newObj.DropBoxAppKey = GetKeyValue("DropBoxAppKey");
            newObj.DropBoxAppSecret = GetKeyValue("DropBoxAppSecret");

            newObj.OneDriveClientId = GetKeyValue("OneDriveClientId");
            newObj.OneDriveClientSecret = GetKeyValue("OneDriveClientSecret");

            newObj.GoogleDriveClientId = GetKeyValue("GoogleDriveClientId");
            newObj.GoogleDriveClientSecret = GetKeyValue("GoogleDriveClientSecret");
            
            var workbool= false;
            bool.TryParse(GetKeyValue("AzureAdMode"), out workbool);
            newObj.AzureAdMode = workbool;
            
            workbool = false;
            bool.TryParse(GetKeyValue("BatchRole"), out workbool);
            newObj.BatchRole = workbool;

            workbool = false;
            bool.TryParse(GetKeyValue("FulltextSearch"), out workbool);
            newObj.FulltextSearch = workbool;
            return newObj;
        }


        private static string GetKeyValue(string key)
        {
            try
            {
                return RoleEnvironment.GetConfigurationSettingValue(key) ?? CloudConfigurationManager.GetSetting(key);
            }
            catch
            {
                return CloudConfigurationManager.GetSetting(key);
            }
        }

    }

}